package com.fiona.core.load;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.fiona.annotation.FionaBean;
import com.fiona.annotation.FionaWrite;
import com.fiona.common.constant.FionaConstant;
import com.fiona.common.constant.FionaSpace;
import com.fiona.core.model.FionaBeanClassModel;
import com.fiona.core.model.FionaBeanModel;
import com.fiona.core.model.FionaMappingModel;

import cn.hutool.core.util.StrUtil;

/**
 * 加载工具类
 */
public class LoadHelper {

	/**
	 * 获取全局存储空间
	 */
	private static FionaSpace constants = FionaSpace.getFionaSpace();

	/**
	 * 获取Bean的名称
	 * 
	 * @param fionaBeanClassModel 集合
	 * @param cls                类
	 * @return 名称
	 */
	public static String getBeanName(FionaBeanClassModel fionaBeanClassModel, Class<?> cls) {
		FionaBean fionaBean = (FionaBean) fionaBeanClassModel.getAnnotation();
		String beanName = fionaBean.value();
		return getBeanName(beanName, cls);
	}

	/**
	 * 获取Bean的名称
	 * 
	 * @param beanName bean名称
	 * @param cls      类
	 * @return 名称
	 */
	public static String getBeanName(String beanName, Class<?> cls) {
		if (beanName == null || beanName.equals("")) {
			beanName = StrUtil.lowerFirst(cls.getSimpleName());
		}
		return beanName;
	}

	/**
	 * 获取字段名
	 * 
	 * @param fionaWrite 资源
	 * @param field      字段
	 * @return 名称
	 */
	public static String getResourceName(FionaWrite fionaWrite, Field field) {
		String filedName = null;
		if (fionaWrite != null) {
			filedName = fionaWrite.value();
		}
		if (filedName == null || filedName.equals("")) {
			filedName = field.getName();
		}
		return filedName;
	}

	/**
	 * 获取所有的加了FionaBean注解的基础数据
	 * 
	 * @return 数据
	 */
	public static List<FionaBeanClassModel> getBeanList() {
		List<FionaBeanClassModel> fionaBeansList = new ArrayList<>();

		Object fionaBeans = constants.getAttr(FionaConstant.FIONA_BEANS);
		if (fionaBeans != null) {
			fionaBeansList = (List<FionaBeanClassModel>) fionaBeans;
		}
		return fionaBeansList;
	}

	/**
	 * 获取所有的bean对象
	 * 
	 * @return 集合
	 */
	public static Map<String, FionaBeanModel> getBeanObjectMap() {
		Map<String, FionaBeanModel> fionaBeanObjects = new HashMap<>();

		Object object = constants.getAttr(FionaConstant.FIONA_BEAN_OBJECTS);
		if (object != null) {
			fionaBeanObjects = (Map<String, FionaBeanModel>) object;
		}
		return fionaBeanObjects;
	}

	/**
	 * 获取所有的加了FionaDao注解的基础数据
	 * 
	 * @return 数据
	 */
	public static List<FionaBeanClassModel> getDaoList() {
		List<FionaBeanClassModel> fionaDaos = new ArrayList<>();

		Object object = constants.getAttr(FionaConstant.FIONA_DAOS);
		if (object != null) {
			fionaDaos = (List<FionaBeanClassModel>) object;
		}
		return fionaDaos;
	}

	/**
	 * 获取所有的加了FionaApi注解的基础数据
	 * 
	 * @return 数据
	 */
	public static List<FionaBeanClassModel> getFionaApiList() {
		List<FionaBeanClassModel> fionaApis = new ArrayList<>();

		Object object = constants.getAttr(FionaConstant.FIONA_API);
		if (object != null) {
			fionaApis = (List<FionaBeanClassModel>) object;
		}
		return fionaApis;
	}
	/**
	 * 获取所有的FionaApi对象。
	 * key格式:IService-serviceName-version-methodName
	 * 
	 * @return 数据
	 */
	public static Map<String, FionaMappingModel> getFionaApiObjectMap() {
		Map<String, FionaMappingModel> fionaApiObjects = new HashMap<>();

		Object object = constants.getAttr(FionaConstant.FIONA_API_OBJECTS);
		if (object != null) {
			fionaApiObjects = (Map<String, FionaMappingModel>) object;
		}
		return fionaApiObjects;
	}

	/**
	 * 获取所有的加了FionaInterceptor注解的基础数据
	 * 
	 * @return 集合
	 */
	public static List<FionaBeanClassModel> getInterceptorList() {
		List<FionaBeanClassModel> interceptors = new ArrayList<>();

		Object object = constants.getAttr(FionaConstant.INTERCEPTORS);
		if (object != null) {
			interceptors = (List<FionaBeanClassModel>) object;
		}
		return interceptors;
	}

	/**
	 * 获取所有的加了FionaAfter注解的基础数据
	 * 
	 * @return 集合
	 */
	public static List<Class> getFionaAfterList() {
		List<Class> fionaAfters = new ArrayList<>();

		Object object = constants.getAttr(FionaConstant.FIONA_AFTERS);
		if (object != null) {
			fionaAfters = (List<Class>) object;
		}
		return fionaAfters;
	}

	/**
	 * 获取扫描出来的类
	 * 
	 * @return 集合
	 */
	public static Set<String> getSacnClassList() {
		Set<String> classList = new HashSet<>();

		Object object = constants.getAttr(FionaConstant.SCAN_ALL_CLASS);
		if (object != null) {
			classList = (Set<String>) object;
		}
		return classList;
	}
}
